<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
	if (!isset($_POST['TimeInvoice'])) {$_POST['TimeInvoice'] = date("Y-m-d");}
	MYDCONN();
	$title = '';
	if ($_GET['Tp'] == 8) { $title = 'Locutorios';} elseif ($_GET['Tp'] == 9) { $title = 'Petty Cash';}
	$GLOBALS['MYT']=$GLOBALS['MYD'];
	form_pr("edtCdcInvoice",'','','post');
	mn_field('hidden','ED','CODE','0','','','','');
	mn_field('hidden','ED','Provider','0','','','','');
	$GET = $GLOBALS['MYD']->db_array("SELECT DATE(a.TimeInvoice) AS TimeInvoice,
						 DATE(a.TimePay) AS TimePay,
						 z.name AS Status,
						 b.name AS office,
						 b.ref AS off,
						 a.Ref 
						 FROM ccenter_invoices AS a
						 INNER JOIN main_offices AS b ON b.ref = {$_SESSION[$SESSOF]} 
						 INNER JOIN hcce_status AS z ON z.StatRef = a.Status AND z.Type = 9
						 WHERE a.Type = {$_GET['Tp']} AND Status IN (1,2)
						 AND  DATE(TimeInvoice) <= DATE('{$_POST['TimeInvoice']}') 
						 ORDER BY TimeInvoice DESC LIMIT 0,1");
$_POST['RefInvoice'] = $GET[1]['Ref'];
	$_POST['Office'] = $GET[1]['off'];
	$_POST['Type'] = $_GET['Tp'];
	$_POST['ArtTYP'] = 0;

	mn_field('hidden','ED','RefInvoice',$GET[1]['Ref'],'','','','');
	mn_field('hidden','ED','Office',$GET[1]['off'],'','','','');
	mn_field('hidden','ED','Type',$_GET['Tp'],'','','','');
	mn_field('hidden','ED','ArtTYP',0,'','','','');

	?>
<div class="title"><?php echo $title;?> - De <?php mn_field('date','ED','TimeInvoice',$GET[1]['TimeInvoice'],'edtCdcInvoice','8','',''); ?>
      <?php mn_field('button','ED','View','Ver','','','','" onclick="document.edtCdcInvoice.action=\'Modules/Centrodecustos/minvoice.php?Tp='.$_GET['Tp'].'\';subform(\'edtCdcInvoice\',\'app\');');
 	      mn_field('button','ED','Print','Imprimir','','8','','" onclick="printdiv(\'app\');'); ?> | Estado: <?php echo $GET[1]['Status'];?>
	  | Loja <input name="text" type="text" disabled value="<?php echo $GET[1]['office'];?>" size="40" /></div>
<table width="100%"  border="0" cellpadding="3" cellspacing="1">
    <tr bgcolor="#D4D4D4">
      <td colspan="4" valign="top">
<div id="invItems" style=" height:450px; overflow:auto">
<?php include($GLOBALS['PAD']."Modules/CentrodeCustos/Lib/list_invotherarticles.php"); ?>
</div>
	</td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td></td>
	  <td colspan="3">Transporte: 0.00<?php 

	   $List = $GLOBALS['MYD']->db_array("SELECT 
					SUM(a.Amount) AS Amount
	  			FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON b.Ref = a.RefInvoice
				WHERE a.Office = {$_SESSION[$SESSOF]}
				AND b.TYPE = 9
				AND b.Status = 2
				AND b.TimeClose <= '{$GET[1]['TimeInvoice']}'
				GROUP BY b.Ref
				ORDER BY b.TimeClose DESC LIMIT 0,1");
		
	$Plafond = $GLOBALS['MYD']->db_array("SELECT 
					SUM(ccenter_petty.Plafond) AS Plaf
	  			FROM ccenter_petty
				WHERE Office = {$_SESSION[$SESSOF]}
				AND sysTime > {$GET[1]['TimeInvoice']} AND ('{$GET[1]['TimePay']}' OR '{$GET[1]['TimePay']}' = '')
				ORDER BY sysTime DESC
				LIMIT 0,1");
		
		if (!isset($Plafond[1])) {
		$Plaf = 0;
		} else {
		$Plaf = $Plafond[1]['Plaf'];
		}
		
		if (!isset($List[1])) {
		$Tranp = 0;
		} else {
		$Tranp = $List[1]['Amount'];
		} 
		'EUR: '.$Plaf-$Tranp; ?></td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
	  <td colspan="3"><b>Total: EUR </b>
	  <?php mn_field('text','ED','Amount',$Amount,'20','14','','INPNUM'); ?></td>
    </tr>
	<tr align="right" bgcolor="#F2F2F2">
	  <td>&nbsp;</td>
    <td colspan="3"><?php //'Saldo: EUR '.$Plaf-$Amount;?>   
	</td>
    </tr>
</table>
 </form>